﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XmlXmlToEditWorker
	{
		
		public static void  doXmlToEditAction(Form form, string xmlToEditName, string xmlToEditAction, string contextIdArray)
		{
			if (xmlToEditName == null)
			{
				throw new InfoJetException("no xmlToEdit name", "XXE01");
			}
			if (xmlToEditAction == null)
			{
				throw new InfoJetException("no xmlToEdit action", "XXE02");
			}
			XmlToEdit xmlToEdit = XmlToEditUtil.findXmlToEditByName(form, xmlToEditName);
            //CID 0708 //2010.11.19 //Begin
            if (xmlToEdit == null)
            {
                return;
            }
            //CID 0708 //2010.11.19 //End
			string component = xmlToEdit.Component;
			if (component == null)
			{
				throw new InfoJetException("xmlToEdit no component", "XXE03");
			}
			//CID 0474 //2009.12.23 //Begin
			form.Logger.LogUpdate(null);
			//CID 0474 //2009.12.23 //End
			XmlToEditContext context = new XmlToEditContext();
			context.XmlToEditName = xmlToEditName;
			context.XmlToEditAction = xmlToEditAction;
			context.XmlToEdit = xmlToEdit;
			context.ContextIdList = ContextUtil.parseContextIdList(contextIdArray);
			context.Doc = form.XmlDoc;
			context.NsResolver = form.XmlNsResolver;
			if (component.Equals(JetConstant.CMP_xOptional))
			{
				if (xmlToEditAction.Equals(JetConstant.CMP_xOptional + "::" + JetConstant.ACT_Remove))
				{
					XmlxOptionalRemoveWorker.xOptionalRemove(form, context);
				}
				else
				{
					XmlxOptionalInsertWorker.xOptionalInsert(form, context);
				}
			}
			else if (component.Equals(JetConstant.CMP_xCollection))
			{
				if (xmlToEditAction.Equals(JetConstant.CMP_xCollection + "::" + JetConstant.ACT_Insert))
				{
					XmlxCollectionInsertWorker.xCollectionInsert(form, context);
				}
				else if (xmlToEditAction.Equals(JetConstant.CMP_xCollection + "::" + JetConstant.ACT_InsertBefore))
				{
					XmlxCollectionInsertBeforeWorker.xCollectionInsertBefore(form, context);
				}
				else if (xmlToEditAction.Equals(JetConstant.CMP_xCollection + "::" + JetConstant.ACT_InsertAfter))
				{
					XmlxCollectionInsertAfterWorker.xCollectionInsertAfter(form, context);
				}
				else if (xmlToEditAction.Equals(JetConstant.CMP_xCollection + "::" + JetConstant.ACT_Remove))
				{
					XmlxCollectionRemoveWorker.xCollectionRemove(form, context);
				}
				else if (xmlToEditAction.Equals(JetConstant.CMP_xCollection + "::" + JetConstant.ACT_RemoveAll))
				{
					XmlxCollectionRemoveAllWorker.xCollectionRemoveAll(form, context);
				}
			}
			else if (component.Equals(JetConstant.CMP_xTextList))
			{
				//CID 0293 //2009.05.28 //|| ACT_Insert
				if (xmlToEditAction.Equals(JetConstant.CMP_xTextList + "::" + JetConstant.ACT_NewItem) ||
					xmlToEditAction.Equals(JetConstant.CMP_xTextList + "::" + JetConstant.ACT_Insert) )
				{
					XmlxTextListNewItemWorker.newItem(form, context);
				}
				else if (xmlToEditAction.Equals(JetConstant.CMP_xTextList + "::" + JetConstant.ACT_DeleteItem))
				{
					XmlxTextListDeleteItemWorker.deleteItem(form, context);
				}
			}
			else if (component.Equals(JetConstant.CMP_xReplace))
			{
				if (xmlToEditAction.Equals(JetConstant.CMP_xReplace + "::" + JetConstant.ACT_Replace))
				{
					XmlxReplaceReplaceWorker.xReplaceReplace(form, context);
				}
			}
		}
	}
}